myApp.controller('ConfigFileController', function ($scope, $stateParams, $http) {
    $scope.info = {};
    $scope.info.selectProject = $stateParams.selectProject;
    $scope.info.selectFile = $stateParams.selectFile;

    $scope.info.selectFileObj = {};
    $scope.info.selectFileObj.key = $scope.info.selectFile.replace("$", "/");
    $scope.info.selectFileObj.value = $scope.info.selectFile;

    $scope.info.selectEnv = $stateParams.selectEnv;
    $scope.info.envList = [];
    $scope.info.projectList = [];
    $scope.info.fileList = [];
    //过滤配置文件
    $scope.info.filterConfig = false;
    if ($stateParams.filterConfig == 'true') {
        $scope.info.filterConfig = true;
    }
    ;

    $scope.left = "";
    $scope.right = "";
    $scope.edit = false;
    //配置文件不存在时,新增文件标识
    $scope.isAdd = false;

    $scope.baseText = "";

    $scope.isRun = true;
    $scope.isCheckIng = true;
    $scope.errorDesc = '';


    $scope.init = function () {


        $http.post(java_url + '/beetle/config/ConfigFileList', {
            selectProject: $scope.info.selectProject,
            selectFile: $scope.info.selectFile,
            selectEnv: $scope.info.selectEnv,
            filterConfig: $scope.info.filterConfig,
            tokenId: getTokenId()
        }).success(function (data) {
            if (data.code == '0') {
                $scope.info.selectProject = data.data.config.selectProject;
                $scope.info.selectFile = data.data.config.selectFile;
                $scope.info.selectEnv = data.data.config.selectEnv;
                $scope.info.envList = data.data.config.envList;
                $scope.info.projectList = data.data.config.projectList;
                $scope.info.fileList = data.data.config.fileList;

                $scope.info.selectFileObj.key = $scope.info.selectFile.replace("$", "/");
                $scope.info.selectFileObj.value = $scope.info.selectFile;

                var configFiles = data.data.configFiles;
                $scope.left = configFiles[0];
                $scope.right = configFiles[1];
                if ($scope.right.exist == '0') {
                    $scope.baseText = $scope.right.text;
                }
                ;

                $scope.disableEditButtonIfNessisary();

            } else {
                // alert(data.desc);
                $scope.errorDesc = data.desc;
                $scope.info.selectProject = data.data.config.selectProject;
                $scope.info.selectFile = data.data.config.selectFile;
                $scope.info.selectEnv = data.data.config.selectEnv;

                $scope.info.selectFileObj.key = $scope.info.selectFile.replace("$", "/");
                $scope.info.selectFileObj.value = $scope.info.selectFile;

                $scope.info.envList = data.data.config.envList;
                $scope.info.projectList = data.data.config.projectList;
                $scope.info.fileList = data.data.config.fileList;
            }
            ;
            $scope.isRun = false;
            $scope.isCheckIng = false;


        });
    };
    $scope.onChange = function () {
        $scope.info.selectFile = $scope.info.selectFileObj.value;
        window.location.href = "#/menu/configFile/" + $scope.info.selectProject
            + "/" + $scope.info.selectFile + "/" + $scope.info.selectEnv + "/" + $scope.info.filterConfig;
    };

    $scope.filterConfig = function () {
        $scope.isRun = true;
        if ($scope.info.filterConfig == false) {
            $scope.info.filterConfig = true;
        } else {
            $scope.info.filterConfig = false;
        }

        window.location.href = "#/menu/configFile/" + $scope.info.selectProject + "//" + $scope.info.selectEnv + "/" + $scope.info.filterConfig;
    };

    $scope.downLoadFile = function () {
        $scope.isRun = true;
        $scope.edit = false;
        $scope.isCheckIng = true;
        $http.post(java_url + '/beetle/config/downLoadFile', {
            selectProject: $scope.info.selectProject,
            selectFile: $scope.info.selectFile,
            selectEnv: $scope.info.selectEnv,
            tokenId: getTokenId()
        }).success(function (data) {
            if (data.code != '0') {
                alert(data.desc);
            }
            ;
            $scope.isRun = false;
            $scope.isCheckIng = false;
            $scope.init();
        });
    };

    /**
     * 添加配置文件
     */
    $scope.addConfig = function () {
        $scope.isAdd = true;
        $scope.right.exist = "0";
        $scope.right.text = "";
    };

    /**
     * 编辑配置文件
     */
    $scope.editConfig = function () {
        if ($scope.edit) {
            $scope.edit = false;
        } else {
            $scope.edit = true;
        }
        ;
    };

    /**
     * 恢复配置文件
     */
    $scope.rollBack = function () {
        $scope.right.text = $scope.baseText;
    };

    /**
     * 保存配置文件
     */
    $scope.saveConfig = function () {
        $scope.edit = false;
        $scope.isRun = true;
        var param = {
            selectProject: $scope.info.selectProject,
            selectFile: $scope.info.selectFileObj.key,
            selectEnv: $scope.info.selectEnv,
            right: JSON.stringify($scope.right),
            tokenId: getTokenId()
        };
        $http.post(java_url + '/beetle/config/saveConfigFile', param).success(function (data) {
            alert(data.desc);
            $scope.isRun = false;
        });
    };

    $scope.disableEditButtonIfNessisary = function () {
        if ($scope.info.selectFile == "password.properties" && $scope.info.selectEnv.indexOf("ops") != -1 && !arrayContains(getUserRole(), 'ops')) {
            $scope.edit = false;
        }
    };


    $scope.init();
});